import Vue from 'vue'
import Router from 'vue-router'
import { getsessionStorage } from './sessionStorage/index'

const Login = () => import('./views/Login.vue')
const Home = () => import('./views/Home.vue')
const HomeIndex = () => import('./components/Home/HomeIndex.vue')
const LoginLogin = () => import('./components/Login/LoginLogin.vue')
const LoginRegister = () => import('./components/Login/Loginregister.vue')
const Activity = () => import('./components/Home/Activity.vue')
const GuestPiece = () => import('./components/Home/GuestPiece.vue')
const UserContent = () => import('./components/Home/UserContent')
const HomeBrand = () => import('./components/Home/HomeBrand')
const Personal = () => import('./components/Home/Personal.vue')
const Customer = () => import('./components/Home/Customer')
const GuestPieceInfo = () => import('./components/Home/GuestPieceInfo.vue')
const UserComment = () => import('./components/Home/UserComment')

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta: {
        keepAlive: true // 需要被缓存
      },
      children: [
        {
          path: '',
          redirect: 'login'
        },
        {
          path: 'login',
          name: 'LoginLogin',
          component: LoginLogin
        },
        {
          path: 'register',
          name: 'Loginregister',
          component: LoginRegister
        }
      ]
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta: {
        keepAlive: true // 需要被缓存
      },
      children: [
        {
          path: 'index',
          name: 'index',
          component: HomeIndex
        },
        {
          path: '',
          redirect: 'index'
        },
        {
          path: 'activity',
          name: 'activity',
          component: Activity
        },
        {
          path: 'guestpiece',
          name: 'guestpiece',
          component: GuestPiece
        },
        {
          path: 'usercontent',
          name: 'usercontent',
          component: UserContent
        },
        {
          path: 'homebrand',
          name: 'homebrand',
          component: HomeBrand
        },
        {
          path: 'personal',
          name: 'personal',
          component: Personal
        },
        {
          path: 'guestpieceinfo/:id',
          name: 'guestpieceinfo',
          component: GuestPieceInfo
        },
        {
          path: 'usercomment',
          name: 'usercomment',
          component: UserComment,
          // meta: { auth: true }
        }
      ]
    },
    {
      path: '/customer',
      name: 'customer',
      component: Customer,
      meta: { auth: true }
    }
  ]
})

router.beforeEach((to, from, next) => {
  //判断是否需要登录
  if (to.matched.some(m => m.meta.auth)) {
    if (getsessionStorage('UserInfo') != undefined) {
    } else if (to.path !== '/') {
      next({ path: '/login' })
      Vue.prototype.$message({
        type: 'error',
        message: '请先登录'
      })
    }
  }
  //判断是否访问的界面是否存在
  if (to.matched.length == 0) {
    from.name ? next({
      name: from.name
    }) : next('/login');
  } else {
    next();
  }
})



export default router;